<?php

use backend\models\MallOrder;
use kartik\export\ExportMenu;
use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\MallOrderSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '商城订单';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="mall-order-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php  echo $this->render('_search', ['model' => $searchModel]); ?>

    <br>
<!--    <p>-->
<!--        --><?//= Html::a('创建订单', ['create'], ['class' => 'btn btn-success']) ?>
<!--    </p>-->

    <?php
    $gridColumns = [
        'id',
        'mid',
        'uid',
        'openid',
        'user.nickname',
        [
            'label' => '订单号',
            'attribute' => 'out_trade_no',
        ],
        'discount',
        'total_fee',
//            'amount',
        'logistic_fee',
        'name',
        'phone',
        'address',
        //'logistics',
        //'order_num',
//            'status',
        [
            'attribute' => 'status',
            'enableSorting' => false,
            'value' => 'statusValue',
            'filter' => MallOrder::attributeValue()['status']
        ],
        //'create_time:datetime',
        [
            'label' => '下单时间',
            'value' => function($model) {
                return date("Y-m-d H:i:s", $model->create_time);
            }
        ],
        [
            'label' => '送达时间',
            'value' => function($model) {
                if ($model->send_time) {
                    return date("Y-m-d H:i:s", $model->send_time);
                } else {
                    return '';
                }
            }
        ],
        [
            'label' => '订单产品详情',
            'value' => function($model) {
                $orderList = MallOrder::orderList($model->out_trade_no);
                if ($orderList) {
                    $str = "";
                    foreach ($orderList as $value) {
                        //获取产品名字
                        $product = MallOrder::productOne($value['pid']);
                        if ($value['value'] == -1) {
                            $value['value'] = '默认';
                        }
                        $str .= "id:{$value['pid']};产品名:{$product['title']},规格:{$value['value']};";
                    }
                    return $str;
                } else {
                    return '';
                }
            }
        ],
        [
            'label' => '分销详情',
            'value' => function($model) {
                $billList = MallOrder::billList($model->out_trade_no);
                $text = '';
                if ($billList) {
                    foreach ($billList as $item) {
                        $user = MallOrder::userOne($item['uid']);
                        $text .= "用户id:{$item['uid']};昵称:{$user['nickname']};关系:{$item['direct_text']};金额:{$item['amount']};奖励比率:{$item['percent']}";
                    }
                    return $text;
                } else {
                    return $text;
                }
            }
        ],
    ];

    echo ExportMenu::widget([
        'dataProvider' => $dataProvider,
        'columns' => $gridColumns,
        'filename' => '商城订单' . date('Ymd'),
        'columnSelectorOptions' => [
            'label' => '选择字段',
        ],

        'exportConfig' => [
            ExportMenu::FORMAT_TEXT => false,
            ExportMenu::FORMAT_PDF => false,
            ExportMenu::FORMAT_EXCEL_X => false,
        ],
    ]);
    ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
//        'filterModel' => $searchModel,
        'pager' => [
            'firstPageLabel' => '第一页',
            'lastPageLabel' => '尾页'
        ],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'mid',
            'uid',
//            'openid',
            [
                'attribute' => 'openid',
                'enableSorting' => false
            ],
            'user.nickname',
//            'out_trade_no',
            [
                'label' => '订单号与订单详情',
                'format' => 'raw',
                'value' => function($model) {
                    return $model->out_trade_no . Html::a('---订单详情', ['mall-order-list/index', 'MallOrderListSearch[out_trade_no]' => $model->out_trade_no], ['class' => '']);
                }
            ],
            'total_fee',
//            'amount',
            'logistic_fee',
            //'name',
            //'phone',
            //'address',
            //'logistics',
            //'order_num',
//            'status',
            [
                'attribute' => 'status',
                'enableSorting' => false,
                'value' => 'statusValue',
                'filter' => MallOrder::attributeValue()['status']
            ],
            'discount',
            'recharge',
            //'create_time:datetime',
            [
                'attribute' => 'create_time',
                'value' => function ($model) {
                    return date('Y-m-d', $model->create_time);
                }
            ],
            [
                'attribute' => 'send_time',
                'enableSorting' => true,
                'value' => function($model) {
                    if ($model->send_time) {
                        return date('Y-m-d', $model->send_time);
                    } else {
                        return '未发货';
                    }
                }
            ],
//            [
//                'label' => '订单产品详情',
//                'value' => function($model) {
//                    var_dump(MallOrder::orderList($model->out_trade_no));
//                }
//            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {update}',

            ],
        ],
    ]); ?>
</div>

<?php
$script = <<< JS
    $(".pagination").append('&nbsp;跳转<input id="invalue" type="text" style="border-radius: 5px;outline: none;border: solid 1px #dddddd;width: 52px;height: 34px;text-align: center;" class="pane"/>页&nbsp;<button id="skip" class="btn btn-default skip ok"">确定</button>');
    
    $('#skip').click(function() {
        var page = $('#invalue').val();
        location.href = "index.php?r=mall-order/index&page="+ page;
    }); 
JS;
$this->registerJs($script);
?>
